<template>
    <div class="goodsEntry">
        <div style="padding:20px">
            <el-button type="primary" @click="beginCheck">开始盘点</el-button>
        </div>
        <el-form :inline="true" style="padding: 20px">
            <el-form-item label="盘点时间：">
                <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                
                v-model="endtime"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="changeTime">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="盘点编号：">
              <el-input style="width: 300px;" v-model="queryData.checkNo" placeholder="请输入盘点编号" size="medium"></el-input>
              
            </el-form-item>
            <el-form-item label="盘点人：">
                <el-input style="width: 300px;" v-model="queryData.username" placeholder="请输入盘点人" size="medium"></el-input>
                
              </el-form-item>
           
            <el-form-item label="">
                <el-button type="primary" size="medium" @click="query">查询</el-button>
                <!-- <el-button type="primary" size="medium" @click="exportData">导出</el-button> -->
                
            </el-form-item>
            
        </el-form>
     <div class="wrapper">
        <el-table class="table" :data="tableData" stripe highlight-current-row border>
            <el-table-column
            type="index"
            width="50">
          </el-table-column>
          <el-table-column align="center" type="selection">
          </el-table-column>
          <el-table-column align="center" prop="checkNo" label="盘点编号">
          </el-table-column>
          <el-table-column align="center" prop="addTime" label="盘点时间">
          </el-table-column>
          <el-table-column align="center" prop="store_name" label="仓库名称">
          </el-table-column>
         
          <el-table-column align="center" prop="skuPcs" label="盘点sku数量">
          </el-table-column>
          <el-table-column align="center" prop="outPcs" label="盘盈">
            </el-table-column>
         <el-table-column align="center" prop="diffPcs" label="盘亏">
          </el-table-column>
          <!-- <el-table-column align="center" prop="diffPcs" label="差额">
          </el-table-column> -->
          <el-table-column align="center" prop="username" label="盘点人">
          </el-table-column>
          
          <el-table-column align="center" prop="description" label="单据备注">
          </el-table-column>
         
          <el-table-column align="center" prop="address" label="操作" width="300">
            <template slot-scope="scope">
              <span class="canceloff-btn" @click="Overflowing(scope.row)" >查看详情</span>
              <!-- diffPcs==0?ifLessDeal==0？1:0 -->
              <span  v-if="scope.row.outPcs != 0?(scope.row.ifOutDeal == 1?1:0):0">已处理盘盈</span>
              <span v-if="scope.row.outPcs != 0?(scope.row.ifOutDeal != 1?1:0):0" class="canceloff-btn" @click="Reportloss(scope.row)" >生成报溢单</span>
              <span  v-if="scope.row.diffPcs !=0?(scope.row.ifLessDeal == 1?1:0):0">已处理盘亏</span>
              <span  v-if="scope.row.diffPcs !=0?(scope.row.ifLessDeal != 1?1:0):0" class="canceloff-btn" @click="transfer(scope.row)">生成报损单</span>
            </template>
          </el-table-column>
        </el-table>
        <!-- <el-pagination background layout="total,prev, pager, next,jumper" :page-size="page.pageSize" :total="page.total" :current-page="page.current"
          @current-change="handleCurrentChange">
        </el-pagination> -->
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.current"
        :page-sizes="[10,20,30]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
      </div>
      <el-dialog title="选择酒厂" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" >
              
              <el-form-item label="选择仓库" :label-width="formLabelWidth" prop="cellarType">
                <el-select v-model="ruleForm.cellarType" placeholder="请选择仓库">
                  <!-- <el-option label="合肥酒窖" value="0"></el-option>
                  <el-option label="温州酒窖" value="1"></el-option> -->
                  <el-option :label="item.storeName" :value="item.id" v-for="item in selectedOptions" :key="item.index"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="sureData('ruleForm')">确 定</el-button>
            </div>
          </el-dialog>
    </div>
  </template>
  
  <script>
    export default {
      data() {
        return {
          height: "auto",
          dialogFormVisible:false,
          queryData: {
            startTime:'',
            endTime:'',
            // goodsId:'',
            checkNo:'',
            username:''
          },
          selectedOptions:[],
          // goodsCates: {
          //   show: false,
          //   tableData: [{}]
          // },
          tableData: [{}],
          
          endtime:'',
          page: {
            pageSize: 10,
            current: 1,
            total: null
          },
          formLabelWidth:'120px',
          ruleForm:{
            cellarType:'',
          },
          rules:{
            cellarType: [
              { required: true, message: '请选择仓库', trigger: 'change' }
            ],
          }
          // numSku:this.$route.params.sku == 'num'?'':this.$route.params.sku
        };
      },
      mounted () {
        console.log(this.$route.params.sku)
        this.area();
        this.tableList();
      },
      computed: {
        
      },
      methods: {
           //初始化数据
      async tableList() {
        try {
            let { data } = await this.$post({
                api: "/show/checkResult",
                data:{
                  current:this.page.current,
                  pageSize:this.page.pageSize,
                  startTime:this.queryData.startTime,
                  endTime:this.queryData.endTime,
                  checkNo:this.queryData.checkNo,
                  username:this.queryData.username,
                  
                }
              });
              if (data.code === 0) {
                this.tableData = data.data;
                this.page.total = data.rdPage.total;
                // this.page.total = data.rdPage.total;
                // console.log(data)
                // this.rawList = data.data;
              } else {
                this.$message({
                  type: "error",
                  message: data.msg
                });
              }
            } catch (e) {
             
            }
        },
        //仓库
       async area() {
          try {
            let { data } = await this.$post({
              api: "/show/store/area"
              
            });
            if (data.code === 0) {
              this.selectedOptions = data.data;
            console.log(data)
            } else {
              this.$message({
                type: "error",
                message: data.msg
              });
            }
          } catch (e) {
            // this.$message({
            //   type: "error",
            //   message: "接口报错"
            // });
          }
       },
       //清除仓库
       async startcheck() {
          try {
            let { data } = await this.$post({
              api: "/start/check",
              data:{
                storeId:this.ruleForm.cellarType
              }
              
            });
            if (data.code === 0) {
             
            
            } else if(data.code == 2){
              this.$message({
                type: "info",
                message: data.msg
              });
            }
          } catch (e) {
            // this.$message({
            //   type: "error",
            //   message: "接口报错"
            // });
          }
       },
       async ingoods(storeId,id) {
          try {
            let { data } = await this.$post({
              api: "/deal/ingoods",
              data:{
                storeId:storeId,
                checkNo:id
              }
              
            });
            if (data.code === 0) {
              this.$message({
                type: "success",
                message: data.msg
              });
              this.tableList();
           
            } else {
              this.$message({
                type: "error",
                message: data.msg
              });
            }
          } catch (e) {
            // this.$message({
            //   type: "error",
            //   message: "接口报错"
            // });
          }
       },
       async outgoods(storeId,id) {
          try {
            let { data } = await this.$post({
              api: "/deal/outgoods",
              data:{
                storeId:storeId,
                checkNo:id

              }
              
            });
            if (data.code === 0) {
              this.$message({
                type: "success",
                message: data.msg
              });
              this.tableList();
           
            } else {
              this.$message({
                type: "error",
                message: data.msg
              });
            }
          } catch (e) {
            // this.$message({
            //   type: "error",
            //   message: "接口报错"
            // });
          }
       },
        //查询
        query() {
          this.tableList();
         },
       
      
        handleCurrentChange(val) {
          this.page.current = val;
           this.tableList();
          console.log(val);
        },
        handleSizeChange(val){
            this.page.pageSize = val;
            this.tableList();
        },
        showCateDialog() {
          this.goodsCates.show = true;
        },
        //时间选择器
        changeTime(arr){
          console.log(arr)
          if(arr != null){
            this.queryData.startTime = arr[0];
            this.queryData.endTime = arr[1];
          }
          
        },
        Overflowing(row){
          console.log(row)
          this.$router.push({path:'/FactoryphysicalCount',query:{id:row.checkNo}})
        },
        //报溢
        Reportloss(row){
          console.log(row)
          if(row.ifOutDeal){
           
            return false;
          }else{
            this.$confirm("是否确定生成报溢单", "系统提示", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning"
                        }).then(() => {
                          this.ingoods(row.storeId,row.checkNo);
                            
                            // this.$message({
                            //   type: "success",
                            //   message: "删除成功!"
                            // });
                        }).catch(() => {
                            this.$message({
                            type: "info",
                            message: "已取消报溢"
                            });
                        });
            
           
          }
        },
        //
        Newwine(row){
            
          this.$router.push('/detailsofnewwine/'+row.sku_no)
          // console.log(22)
        },
        //报损
        transfer(row){
          if(row.ifLessDeal){
            return false;
            }else{
              
              this.$confirm("是否确定生成报损单", "系统提示", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning"
                        }).then(() => {
                          this.outgoods(row.storeId,row.checkNo);
                            
                            // this.$message({
                            //   type: "success",
                            //   message: "删除成功!"
                            // });
                        }).catch(() => {
                            this.$message({
                            type: "info",
                            message: "已取消报损"
                            });
                        });
            }
        
        },
        beginCheck(){
          this.dialogFormVisible = true;
        },
        sureData(formName){
          this.$refs[formName].validate((valid) => {
          if (valid) {
            this.startcheck();
             this.$router.push({path:'/beginCheck/',query:{id:this.ruleForm.cellarType}})
          } else {
            console.log('error submit!!');
            return false;
          }
         })
          
        }
      }
    };
  </script>
  
  <style lang="scss" scoped>
    .query {
      overflow: hidden;
      border-bottom: 1px solid #eeeeee;
      &-item {
        float: left;
        margin: 15px 20px;
      }
      .el-input {
        width: 150px;
      }
      .el-button {
        margin: 0 15px;
        &:last-child {
          margin-right: 0;
        }
      }
    }
  
    .entry-info {
      padding: 15px 20px;
      border-bottom: 1px solid #eeeeee;
    }
  
    .goods-category {
      line-height: 32px;
      span {
        color: #0033ff;
        text-decoration: underline;
        cursor: pointer;
      }
    }
  
    .goods-count {
      line-height: 32px;
    }
  
    .goods-id {
      display: flex;
      justify-content: space-between;
      padding: 0 40px;
      border-left: 1px solid #000;
      border-right: 1px solid #000;
      span {
        position: relative;
        display: inline-block;
        padding: 8px 15px;
        border: 1px solid #333;
        &:after {
          content: "";
          position: absolute;
          top: -6px;
          left: -6px;
          width: 100%;
          height: 100%;
          padding: 6px;
          border: 1px solid #333;
        }
      }
    }
  
    .wrapper {
      padding: 10px 15px;
    }
  
    .canceloff-btn {
      color: #0033ff;
      cursor: pointer;
    }
  </style>